home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / fax / src / faxalter / faxalter.c++ next >
C/C++ Source or Header  |  1994-08-01  |  6KB  |  235 lines

  1. /*    $Header: /usr/people/sam/fax/faxalter/RCS/faxalter.c++,v 1.13 1994/03/09 18:47:20 sam Rel $ */
  2. /*
  3.  * Copyright (c) 1990, 1991, 1992, 1993, 1994 Sam Leffler
  4.  * Copyright (c) 1991, 1992, 1993, 1994 Silicon Graphics, Inc.
  5.  *
  6.  * Permission to use, copy, modify, distribute, and sell this software and 
  7.  * its documentation for any purpose is hereby granted without fee, provided
  8.  * that (i) the above copyright notices and this permission notice appear in
  9.  * all copies of the software and related documentation, and (ii) the names of
  10.  * Sam Leffler and Silicon Graphics may not be used in any advertising or
  11.  * publicity relating to the software without the specific, prior written
  12.  * permission of Sam Leffler and Silicon Graphics.
  13.  * 
  14.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
  15.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
  16.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
  17.  * 
  18.  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  19.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  20.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  21.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
  22.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
  23.  * OF THIS SOFTWARE.
  24.  */
  25. #include "FaxClient.h"
  26. #include "StrArray.h"
  27. #include "config.h"
  28.  
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <stdarg.h>
  32.  
  33. class faxAlterApp : public FaxClient {
  34. private:
  35.     fxStr    appName;        // for error messages
  36.     fxStr    notify;
  37.     fxStr    tts;
  38.     fxStr    killtime;
  39.     fxStr    maxdials;
  40.     fxStrArray    jobids;
  41.  
  42.     void usage();
  43.     void printError(const char* fmt, ...);
  44.     void printWarning(const char* fmt, ...);
  45. public:
  46.     faxAlterApp();
  47.     ~faxAlterApp();
  48.  
  49.     void initialize(int argc, char** argv);
  50.     void open();
  51.  
  52.     void recvConf(const char* cmd, const char* tag);
  53.     void recvEof();
  54.     void recvError(const int err);
  55. };
  56. faxAlterApp::faxAlterApp() {}
  57. faxAlterApp::~faxAlterApp() {}
  58.  
  59. void
  60. faxAlterApp::initialize(int argc, char** argv)
  61. {
  62.     int c, n;
  63.  
  64.     appName = argv[0];
  65.     u_int l = appName.length();
  66.     appName = appName.tokenR(l, '/');
  67.     while ((c = getopt(argc, argv, "a:h:k:n:t:DQRpv")) != -1)
  68.     switch (c) {
  69.     case 'D':            // set notification to when done
  70.         notify = "when done";
  71.         break;
  72.     case 'Q':            // no notification (quiet)
  73.         notify = "none";
  74.         break;
  75.     case 'R':            // set notification to when requeued
  76.         notify = "when requeued";
  77.         break;
  78.     case 'a':            // send at specified time
  79.         tts = optarg;
  80.         break;
  81.     case 'h':            // server's host
  82.         setHost(optarg);
  83.         break;
  84.     case 'k':            // kill job at specified time
  85.         killtime = optarg;
  86.         break;
  87.     case 'n':            // set notification
  88.         if (strcmp(optarg, "done") == 0)
  89.         notify = "when done";
  90.         else if (strcmp(optarg, "requeued") == 0)
  91.         notify = "when requeued";
  92.         else
  93.         notify = optarg;
  94.         break;
  95.     case 'p':            // send now (push)
  96.         tts = "now";
  97.         break;
  98.     case 't':            // set max number of retries
  99.         n = atoi(optarg);
  100.         if (n < 0)
  101.         fxFatal("Bad number of retries for -t option: %s", optarg);
  102.         maxdials = fxStr(n, "%d");
  103.         break;
  104.     case 'v':            // trace protocol
  105.         setVerbose(TRUE);
  106.         break;
  107.     case '?':
  108.         usage();
  109.     }
  110.     if (optind >= argc)
  111.     usage();
  112.     if (tts == "" && notify == "" && killtime == "" && maxdials == "")
  113.     fxFatal("No job parameters specified for alteration.");
  114.     setupUserIdentity();
  115.     for (; optind < argc; optind++)
  116.     jobids.append(argv[optind]);
  117. }
  118.  
  119. void
  120. faxAlterApp::usage()
  121. {
  122.     fxFatal("usage: %s"
  123.       " [-h server-host]"
  124.       " [-a time]"
  125.       " [-k time]"
  126.       " [-n notify]"
  127.       " [-t tries]"
  128.       " [-p]"
  129.       " [-DQR]"
  130.       " jobID...", (char*) appName);
  131. }
  132.  
  133. void
  134. faxAlterApp::open()
  135. {
  136.     if (callServer()) {
  137.     for (int i = 0; i < jobids.length(); i++) {
  138.         fxStr line = jobids[i] | ":" | getSenderName();
  139.         // do notify first 'cuz setting tts causes q rescan
  140.         if (notify != "")
  141.         sendLine("alterNotify", (char*)(line | ":" | notify));
  142.         if (tts != "")
  143.         sendLine("alterTTS", (char*)(line | ":" | tts));
  144.         if (killtime != "")
  145.         sendLine("alterKillTime", (char*)(line | ":" | killtime));
  146.         if (maxdials != "")
  147.         sendLine("alterMaxDials", (char*)(line | ":" | maxdials));
  148.     }
  149.     sendLine(".\n");
  150.     startRunning();
  151.     } else
  152.     fxFatal("Could not call server.");
  153. }
  154.  
  155. void
  156. faxAlterApp::printError(const char* va_alist ...)
  157. #define    fmt va_alist
  158. {
  159.     va_list ap;
  160.     va_start(ap, va_alist);
  161.     fprintf(stderr, "%s: ", (char*) appName);
  162.     vfprintf(stderr, fmt, ap);
  163.     va_end(ap);
  164.     fputs("\n", stderr);
  165.     exit(-1);
  166. }
  167. #undef fmt
  168.  
  169. void
  170. faxAlterApp::printWarning(const char* va_alist ...)
  171. #define    fmt va_alist
  172. {
  173.     va_list ap;
  174.     va_start(ap, va_alist);
  175.     fprintf(stderr, "%s: Warning, ", (char*) appName);
  176.     vfprintf(stderr, fmt, ap);
  177.     va_end(ap);
  178.     fputs("\n", stderr);
  179. }
  180. #undef fmt
  181.  
  182. #define isCmd(s)    (strcasecmp(s, cmd) == 0)
  183.  
  184. void
  185. faxAlterApp::recvConf(const char* cmd, const char* tag)
  186. {
  187.     if (isCmd("altered")) {
  188. //    printf("Job %s altered.\n", tag);
  189.     } else if (isCmd("notQueued")) {
  190.     printf("Job %s not altered: it was not queued.\n", tag);
  191.     } else if (isCmd("jobLocked")) {
  192.     printf("Job %s not altered: it is being sent.\n", tag);
  193.     } else if (isCmd("openFailed")) {
  194.     printf("Job %s not altered: open failed; check SYSLOG.\n", tag);
  195.     } else if (isCmd("sOwner")) {
  196.     printf(
  197.     "Job %s not altered: could not establish server process owner.\n",
  198.         tag);
  199.     } else if (isCmd("jobOwner")) {
  200.     printf(
  201.     "Job %s not altered: you neither own it nor are the fax user.\n",
  202.         tag);
  203.     } else if (isCmd("error")) {
  204.     printf("%s\n", tag);
  205.     } else {
  206.     printf("Unknown status message \"%s:%s\"\n", cmd, tag);
  207.     }
  208. }
  209.  
  210. void
  211. faxAlterApp::recvEof()
  212. {
  213.     stopRunning();
  214. }
  215.  
  216. void
  217. faxAlterApp::recvError(const int err)
  218. {
  219.     printf("Fatal socket read error: %s\n", strerror(err));
  220.     stopRunning();
  221. }
  222.  
  223. #include <Dispatch/dispatcher.h>
  224.  
  225. extern "C" int
  226. main(int argc, char** argv)
  227. {
  228.     faxAlterApp app;
  229.     app.initialize(argc, argv);
  230.     app.open();
  231.     while (app.isRunning())
  232.     Dispatcher::instance().dispatch();
  233.     return 0;
  234. }
  235.